package com.yxx.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxx.mapper.ChineseMedicineMapper;
import com.yxx.mapper.SetMedicineMapper;
import com.yxx.medicinecommon.constant.MessageConstant;
import com.yxx.medicinecommon.constant.StatusConstant;
import com.yxx.medicinecommon.exception.DeletionNotAllowedException;
import com.yxx.medicinecommon.exception.SetmedicineEnableFailedException;
import com.yxx.medicinecommon.result.PageResult;
import com.yxx.medicinepojo.dto.ChineseMedicineDTO;
import com.yxx.medicinepojo.dto.ChineseMedicinePageQueryDTO;
import com.yxx.medicinepojo.entity.ChineseMedicine;
import com.yxx.medicinepojo.entity.ChineseWesternMedicine;
import com.yxx.medicinepojo.entity.SetMedicine;
import com.yxx.medicinepojo.vo.ChineseMedicineVO;
import com.yxx.medicinepojo.vo.ChineseWesternMedicineVO;
import com.yxx.service.ChineseMedicineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author yuxiangxun
 * @date 2024/9/21
 * @apiNote
 */
@Service
@Slf4j
public class ChineseMedicineServiceImpl implements ChineseMedicineService {

    @Autowired
    private ChineseMedicineMapper chineseMedicineMapper;

    @Autowired
    private SetMedicineMapper setMedicineMapper;

    /**
     * 中药管理分页
     * @param chineseMedicinePageQueryDTO
     * @return
     */
    @Override
    public PageResult chineseMedicinePage(ChineseMedicinePageQueryDTO chineseMedicinePageQueryDTO) {
        PageHelper.startPage(chineseMedicinePageQueryDTO.getPage(),chineseMedicinePageQueryDTO.getPageSize());
        Page<ChineseMedicineVO> chineseMedicineVOS = chineseMedicineMapper.pageQueryChineseMedicine(chineseMedicinePageQueryDTO);
        log.info("chineseMedicinePageVOS:{}",chineseMedicineVOS.toString());
        return new PageResult(chineseMedicineVOS.getTotal(),chineseMedicineVOS.getResult());

    }
    /**
     * 根据分类id查询中药
     * @param categoryId
     * @return
     */
    @Override
    public List<ChineseMedicineVO> list(Long categoryId, String name) {
        ChineseMedicine chineseMedicine= ChineseMedicine.builder()
                .status(StatusConstant.ENABLE)
                .categoryId(categoryId)
                .name(name)
                .build();
        ChineseMedicineVO chineseMedicineVO = new ChineseMedicineVO();
        BeanUtils.copyProperties(chineseMedicine,chineseMedicineVO);
        //查询操作
        List<ChineseMedicineVO> list = chineseMedicineMapper.list(chineseMedicineVO);
        return list;
    }
    /**
     * 中药上架下架
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void StartOrStop(Integer status, Long id) {
        //药品下架时，判断组合药内是否有关联的药品，如果有关联这个组合药，且这个组合药在上架状态，则不能下架
        if(StatusConstant.DISABLE.equals(status)){
            //select s.* from setmedicine s left join setmedicine_medicine sm on s.id = sm.setmedicine_id where sm.medicine_id = #{id}
            List<SetMedicine> setmecicinesList = setMedicineMapper.getByChineseWesternMedicineById(id);
            if(setmecicinesList != null && setmecicinesList.size() > 0){
                setmecicinesList.forEach(setMedicine -> {
                    if(StatusConstant.ENABLE.equals(setMedicine.getStatus())){
                        throw new SetmedicineEnableFailedException(MessageConstant.CHINESE_MEDICINE_ENABLE_FAILED);
                    }
                });
            }
        }

        ChineseMedicine chineseMedicine = ChineseMedicine.builder()
                .id(id)
                .status(status)
                .build();
        chineseMedicineMapper.update(chineseMedicine);
    }
    /**
     * 新增药品
     * @param chineseMedicineDTO
     */
    @Override
    public void save(ChineseMedicineDTO chineseMedicineDTO) {
        ChineseMedicine chineseMedicine = new ChineseMedicine();
        BeanUtils.copyProperties(chineseMedicineDTO,chineseMedicine);
        chineseMedicineMapper.insert(chineseMedicine);
    }
    /**
     * 修改中药
     * @param chineseMedicineDTO
     */
    @Override
    public void updateChineseMedicine(ChineseMedicineDTO chineseMedicineDTO) {
        ChineseMedicine chineseMedicine = new ChineseMedicine();
        BeanUtils.copyProperties(chineseMedicineDTO,chineseMedicine);
        chineseMedicineMapper.update(chineseMedicine);
    }
    /**
     * 回显数据，根据id查询中药
     * @param id
     * @return
     */
    @Override
    public ChineseMedicineVO ChineseMedicineSelectUpdateById(Long id) {
        //根据id查询药品信息
        ChineseMedicine chineseMedicine = chineseMedicineMapper.ChineseMedicineSelectUpdateById(id);
        ChineseMedicineVO chineseMedicineVO = new ChineseMedicineVO();
        BeanUtils.copyProperties(chineseMedicine,chineseMedicineVO);
        return chineseMedicineVO;
    }
    /**
     * 批量删除中药
     * @param ids
     */
    @Override
    @Transactional
    public void deleteChineseMedicine(List<Long> ids) {
        //判断当前中药是否能删除  -->  是否存在上线中的药品  有上线的药品不能删除
        for(Long id : ids){
            ChineseMedicine chineseMedicine = chineseMedicineMapper.ChineseMedicineById(id);
            //果果获取到当前中品信息中的状态为上线状态，抛出自定义异常，显示药品为上线状态不能删除
            if(StatusConstant.ENABLE.equals(chineseMedicine.getStatus())){
                throw new DeletionNotAllowedException(MessageConstant.CHINESE_MEDICINE_ON_SALE);
            }
        }

        //判断当前中药品是否能删除 --> 是否关联了组合药，关联了组合药不能删除
        List<Long> setmedicineId_Medicine = setMedicineMapper.setMedicineById(ids);
        if(setmedicineId_Medicine != null && setmedicineId_Medicine.size() > 0){
            //当前中药关联了组合药，抛出自定义异常
            throw new DeletionNotAllowedException(MessageConstant.CHINESE_CATEGORY_BE_RELATED_BY_SETMEDICINE_MEDICINE);
        }

        chineseMedicineMapper.deleteByIds(ids);
    }
}
